/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AtivacaoDialog.java
 *
 * Created on Nov 24, 2011, 11:37:39 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControleAjudaCusto;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.gui.financeiro.AjudaCustoDialog;
import br.com.tbp.gui.table.AjudaCustoTableModel;
import br.com.tbp.gui.table.AjudaCustoTableRenderer;
import br.com.tbp.gui.table.JTableUtils;
import br.com.tbp.model.domain.financeiro.AjudaCusto;
import br.com.tbp.model.domain.financeiro.StatusAjuda;
import br.com.tbp.utils.ImageUtils;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego
 */
public class AjudaCustoFrame extends javax.swing.JFrame {

    private AjudaCustoTableModel tableModel;
    private ControleAjudaCusto controle = new ControleAjudaCusto();
    private static final int FILTRO_PERIODO = 0;
    private static final int FILTRO_CONSULTOR = 1;
    private static final int FILTRO_STATUS = 2;
    private AjudaCustoTableRenderer tableRenderer = new AjudaCustoTableRenderer();

    public AjudaCustoFrame(String title) {
        super(title);
        initComponents();
        setIconImage(ImageUtils.createIcon("sistema.png").getImage());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setLocationRelativeTo(null);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        inicioField = new org.jdesktop.swingx.JXDatePicker();
        fimField = new org.jdesktop.swingx.JXDatePicker();
        fecharButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        pesquisaPeriodoButton = new javax.swing.JButton();
        qtdeLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        consultorField = new javax.swing.JTextField();
        pesquisarConsultorButton = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        statusCombo = new javax.swing.JComboBox();
        pesquisarStatusButton = new javax.swing.JButton();
        adicionarButton = new javax.swing.JButton();
        removerButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        table.setForeground(new java.awt.Color(0, 0, 0));
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        table.setSelectionBackground(new java.awt.Color(121, 177, 231));
        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        jLabel1.setText("Data da liberação entre:");

        fecharButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/delete.png"))); // NOI18N
        fecharButton.setText("Fechar");
        fecharButton.setToolTipText("Sair");
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharButtonActionPerformed(evt);
            }
        });

        pesquisaPeriodoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/magnifier.png"))); // NOI18N
        pesquisaPeriodoButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        pesquisaPeriodoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisaPeriodoButtonActionPerformed(evt);
            }
        });

        qtdeLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        qtdeLabel.setText("0 entradas localizadas");

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel2.setText("Consultor:");

        consultorField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultorFieldActionPerformed(evt);
            }
        });

        pesquisarConsultorButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/magnifier.png"))); // NOI18N
        pesquisarConsultorButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        pesquisarConsultorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarConsultorButtonActionPerformed(evt);
            }
        });

        jSeparator3.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel3.setText("Status:");

        statusCombo.setModel(new DefaultComboBoxModel(br.com.tbp.model.domain.financeiro.StatusAjuda.values()));

        pesquisarStatusButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/magnifier.png"))); // NOI18N
        pesquisarStatusButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        pesquisarStatusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarStatusButtonActionPerformed(evt);
            }
        });

        adicionarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/plus_16.png"))); // NOI18N
        adicionarButton.setText("Nova Ajuda");
        adicionarButton.setToolTipText("Nova ajuda de custo");
        adicionarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarButtonActionPerformed(evt);
            }
        });

        removerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/trash.png"))); // NOI18N
        removerButton.setText("Remover Ajuda");
        removerButton.setToolTipText("Para remover uma ajuda de custo, selecione uma entrada na tabela");
        removerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removerButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 1059, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(adicionarButton)
                .addGap(18, 18, 18)
                .addComponent(removerButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 485, Short.MAX_VALUE)
                .addComponent(qtdeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1035, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fecharButton)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(inicioField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(fimField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(12, 12, 12)
                        .addComponent(pesquisaPeriodoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(consultorField, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(pesquisarConsultorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(statusCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(pesquisarStatusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(inicioField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(pesquisaPeriodoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(fimField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(consultorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pesquisarConsultorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(pesquisarStatusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(statusCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(qtdeLabel)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(adicionarButton)
                        .addComponent(removerButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fecharButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
        dispose();
    }//GEN-LAST:event_fecharButtonActionPerformed

    private void pesquisaPeriodoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisaPeriodoButtonActionPerformed
        findAjudaCusto(FILTRO_PERIODO);
    }//GEN-LAST:event_pesquisaPeriodoButtonActionPerformed

    private void pesquisarConsultorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarConsultorButtonActionPerformed
        findAjudaCusto(FILTRO_CONSULTOR);
    }//GEN-LAST:event_pesquisarConsultorButtonActionPerformed

    private void pesquisarStatusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarStatusButtonActionPerformed
        findAjudaCusto(FILTRO_STATUS);
    }//GEN-LAST:event_pesquisarStatusButtonActionPerformed

    private void adicionarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarButtonActionPerformed
        AjudaCustoDialog dialog = new AjudaCustoDialog(this, "TBP - Ajuda de Custo", true);
        dialog.setAjudaCusto(new AjudaCusto());
        dialog.setNovaAjudaCusto(true);
        dialog.setVisible(true);
    }//GEN-LAST:event_adicionarButtonActionPerformed

    private void consultorFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultorFieldActionPerformed
        findAjudaCusto(FILTRO_CONSULTOR);
    }//GEN-LAST:event_consultorFieldActionPerformed

    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
        if (evt.getClickCount() == 2) {
            int row = table.getSelectedRow();
            if (row >= 0) {
                AjudaCusto ajudaCusto = tableModel.getAjudaCusto(row);
                AjudaCustoDialog dialog = new AjudaCustoDialog(this, "TBP - Ajuda de Custo", true);
                dialog.setAjudaCusto(ajudaCusto);
                dialog.setNovaAjudaCusto(false);
                dialog.setVisible(true);
            }
        }
    }//GEN-LAST:event_tableMouseClicked

    private void removerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removerButtonActionPerformed
        int row = table.getSelectedRow();
        if (row >= 0) {
            AjudaCusto ajuda = tableModel.getAjudaCusto(row);
            if (ajuda != null) {
                Object[] opcoes = {"Sim", "Não"};
                int resposta = JOptionPane.showOptionDialog(this,
                        "Confirma remover a ajuda de custo nº " + ajuda.getId() + " ?",
                        "Atenção", JOptionPane.DEFAULT_OPTION, 2,
                        null, opcoes, opcoes[1]);
                if (resposta == 0) {
                    try {
                        controle.remover(ajuda);
                        getTableModel().removerAjudaCusto(ajuda);
                    } catch (PersistenciaException pex) {
                        System.out.println(pex.getMessage());
                    }
                }
            }
        }
    }//GEN-LAST:event_removerButtonActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                AjudaCustoFrame dialog = new AjudaCustoFrame("TBP - Ajuda de Custo");
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton adicionarButton;
    private javax.swing.JTextField consultorField;
    private javax.swing.JButton fecharButton;
    private org.jdesktop.swingx.JXDatePicker fimField;
    private org.jdesktop.swingx.JXDatePicker inicioField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JButton pesquisaPeriodoButton;
    private javax.swing.JButton pesquisarConsultorButton;
    private javax.swing.JButton pesquisarStatusButton;
    private javax.swing.JLabel qtdeLabel;
    private javax.swing.JButton removerButton;
    private javax.swing.JComboBox statusCombo;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables

    private void findAjudaCusto(int filtro) {
        try {
            List<AjudaCusto> ajudaCustoList = null;
            if (FILTRO_PERIODO == filtro) {
                ajudaCustoList = controle.pesquisar(inicioField.getDate(), fimField.getDate());
            } else if (FILTRO_CONSULTOR == filtro) {
                ajudaCustoList = controle.pesquisar(consultorField.getText());
                consultorField.requestFocus();
                consultorField.selectAll();
            } else if (FILTRO_STATUS == filtro) {
                StatusAjuda status = (StatusAjuda) statusCombo.getSelectedItem();
                ajudaCustoList = controle.pesquisar(status);
            }
            tableModel = new AjudaCustoTableModel(ajudaCustoList);
            table.setModel(tableModel);

            adjustTable();

            if (ajudaCustoList.size() == 1) {
                qtdeLabel.setText("1 entrada localizada");
            } else {
                qtdeLabel.setText(ajudaCustoList.size() + " entradas localizadas");
            }
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }

    private void adjustTable() {
        JTableUtils.fitAllColumns(table, tableRenderer);
    }

    public AjudaCustoTableModel getTableModel() {
        return tableModel;
    }
}
